﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.Coolzon.DesignServer.Domain.Services
{
    public class SolutionService
    {
        /// <summary>
        /// 创建方案
        /// </summary>
        /// <param name="name"></param>
        /// <param name="caption"></param>
        /// <param name="type"></param>
        /// <param name="rtVersion"></param>
        /// <param name="userIdentity"></param>
        /// <returns></returns>
        public SingleResult<Entities.Solution> Create(string name, string caption, string type, string rtVersion, string userIdentity)
        {
            using (DSDBContext dbContext = new DSDBContext())
            {
                Repositories.EntityRepository<Entities.Solution> rep = new Repositories.EntityRepository<Entities.Solution>(dbContext);
                if (rep.Get(p => p.Name == name) != null)
                {
                    Common.Logger.ErrorToTag("SolutionService", string.Format("用户:{0},创建{1}的方案:{2}失败方案名称已存在！", userIdentity, type, name));
                    return new SingleResult<Entities.Solution>(false, 1, "方案名称已存在", null);
                }

                Entities.Solution entity = new Entities.Solution();
                entity.Id = Guid.NewGuid();
                entity.CreatedTime = DateTime.Now;
                entity.Creator = userIdentity;
                entity.Name = name;
                entity.Caption = caption;
                entity.Type = type;
                entity.Version = "1.0.0.0";
                entity.RTVersion = rtVersion;
                entity.IsDeleted = false;
                entity.IsEnable = true;
                rep.Create(entity);

                if (dbContext.SaveChanges() > 0)
                {
                    Common.Logger.InfoToTag("SolutionService", string.Format("用户:{0},创建{1}的方案:{2}成功！", userIdentity, type, name));
                    return new SingleResult<Entities.Solution>(true, 0, string.Empty, entity);
                }

                Common.Logger.ErrorToTag("SolutionService", string.Format("用户:{0},创建{1}的方案:{2}失败！", userIdentity, type, name));
                return new SingleResult<Entities.Solution>(false, 1, string.Empty, null);
            }
        }

        /// <summary>
        /// 获取用户可用的工作空间
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public ListResult<Entities.Solution> Find(string userAccount)
        {
            using (DSDBContext dbContext = new DSDBContext())
            {
                Repositories.EntityRepository<Entities.Solution> rep = new Repositories.EntityRepository<Entities.Solution>(dbContext);
                List<Entities.Solution> list = rep.GetList(p => p.IsDeleted == false && p.IsEnable);

                Common.Logger.InfoToTag("SolutionService", string.Format("获取用户{0}可用的工作空间成功！", userAccount));
                return new ListResult<Entities.Solution>(true, 0, string.Empty, list);
            }
        }
    }
}